package com.example.terminalservice.security;

import com.example.terminalservice.mapper.MenuMapper;
import com.example.terminalservice.pojo.User;
import com.example.terminalservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 28142
 * @description UserDetailsServiceImpl
 * @date 2023/8/13 15:43
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByUsername(username);
        // 判断用户是否存在
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        // 获取用户所有权限
        List<String> authority = menuMapper.getAuthorityByUserId(user.getId());
        user.setAuthorityList(authority);
        SecurityUser securityUser = new SecurityUser(user);
        return securityUser;
    }
}
